package xin.marcher.module.diplomacy.manager.custom.db;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Sets;
import org.springframework.stereotype.Service;
import xin.marcher.module.common.enums.DelFlagEnum;
import xin.marcher.module.diplomacy.domain.entity.ItemInfoDO;
import xin.marcher.module.diplomacy.manager.common.cache.RedisStringCache;
import xin.marcher.module.diplomacy.manager.common.db.AbstractRedisStringDatabase;
import xin.marcher.module.diplomacy.mapper.ItemInfoMapper;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

/**
 * item综合信息
 */
@Service("itemCollectStringDatabase")
public class ItemCollectStringDatabase extends AbstractRedisStringDatabase<ItemInfoDO> {

    @Resource
    private ItemInfoMapper itemInfoMapper;

    /**
     * 表名
     */
    private static final String TABLE_NAME = "item_info";

    /**
     * 商品ID
     */
    public static final String ITEM_ID = "itemId";

    /**
     * 主表
     *
     * @return
     */
    @Override
    public String getTableName() {
        return TABLE_NAME;
    }

    /**
     * 扫描的表
     *
     * @return
     */
    @Override
    public Set<String> getTableNameSet() {
        return Sets.newHashSet(getTableName());
    }


    /**
     * 获取表数据
     *
     * @param tableFieldsMap key 数据库字段，value 字段值
     * @param queryType      查询类型，多数据查询时区分
     * @return java.util.Optional<xin.marcher.module.diplomacy.domain.entity.ItemInfoDO>
     */
    @Override
    public Optional<ItemInfoDO> getTableData(Map<String, Object> tableFieldsMap, String queryType) {
        if (tableFieldsMap.containsKey(ITEM_ID)) {
            String itemId = (String) tableFieldsMap.get(ITEM_ID);
            QueryWrapper<ItemInfoDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ITEM_ID", itemId);
            ItemInfoDO itemInfoDO = itemInfoMapper.selectOne(queryWrapper);
            if (itemInfoDO.getDelFlag().equals(DelFlagEnum.EFFECTIVE.getCode())) {
                return Optional.of(itemInfoDO);
            }
            return Optional.empty();
        }
        throw new UnsupportedOperationException();
    }

    /**
     * 获取表集合数据
     *
     * @param tableFieldsMap
     * @param queryType
     * @return java.util.Optional<java.util.List < T>>
     */
    @Override
    public Optional<List<ItemInfoDO>> listTableData(Map<String, Object> tableFieldsMap, String queryType) {
        return Optional.empty();
    }

    /**
     * 根据ID批量获取数据
     *
     * @param idSet     表主键ID集合
     * @param queryType 查询类型，多数据查询时区分
     * @return java.util.Optional<xin.marcher.module.diplomacy.manager.common.source.RedisStringSource < xin.marcher.module.diplomacy.domain.entity.ItemInfoDO>>
     */
    @Override
    public Optional<RedisStringCache<ItemInfoDO>> listTableDataByIdSet(Set<Long> idSet, String queryType) {
        QueryWrapper<ItemInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("ID", idSet);
        List<ItemInfoDO> itemInfoDOList = itemInfoMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(itemInfoDOList)) {
            return Optional.empty();
        }
        RedisStringCache redisStringCache = new RedisStringCache();
        List<ItemInfoDO> addList = itemInfoDOList.stream().filter(doBase -> doBase.getDelFlag().equals(DelFlagEnum.EFFECTIVE.getCode())).collect(toList());
        Set<String> deleteSet = itemInfoDOList.stream().filter(doBase -> doBase.getDelFlag().equals(DelFlagEnum.DISABLED.getCode())).map(doBase -> String.valueOf(doBase.getItemId())).collect(toSet());
        redisStringCache.setDeleteSet(deleteSet);
        redisStringCache.setAddList(addList);
        return Optional.of(redisStringCache);
    }

    @Override
    public Optional<RedisStringCache<ItemInfoDO>> listTableDataByIdSet(List<Long> idSet, String queryType) {
        return Optional.empty();
    }

}
